package com.cmys.money.cloudshop.model.product;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import com.cmys.money.cloudshop.model.system.ImageFile;
import com.cmys.money.cloudshop.util.excel.PoiExcelAnno;

@Entity
@Table(name = "tb_productinfo")
public class ProductInfo implements Serializable {

	private static final long serialVersionUID = 3276733884610479322L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(length = 20, nullable = false)
	private Long id;

	/** 货号 **/
	@Column(length = 20, nullable = true)
	private String code;

	/** 产品名称 **/
	@Column(length = 20, nullable = true)
	@PoiExcelAnno(colNum=0, colName="名称", colType="string")
	private String name;

	/** 型号 **/
	@Column(length = 20, nullable = true)
	private String model;

	/** 底价(采购进来的价格) **/
	@Column(length = 20, nullable = true)
	private Float baseprice;

	/** 市场价 **/
	@Column(length = 20, nullable = true)
	private Float marketprice;

	/** 销售价 **/
	@Column(length = 20, nullable = true)
	private Float sellprice;

	/** 重量 单位:克 **/
	@Column(length = 20, nullable = true)
	private Long weight;

	/** 产品简介 **/
	@Column(nullable = true)
	@Lob
	@Basic(fetch = FetchType.LAZY) // 大字段延迟加载
	private String description;

	/** 购买说明 **/
	@Column(nullable = true)
	@Lob
	@Basic(fetch = FetchType.LAZY)
	private String buyexplain;

	/** 是否可见 **/
	@Column(nullable = true)
	private Boolean visible = true;

	/** 上架日期 **/
	@Column(nullable = true)
	@Temporal(TemporalType.TIMESTAMP) // TemporalType：DATE(xxxx-xx-xx)，TIMESTAMP(xxxx-xx-xx xx:xx:xx)
	private Date createdate;

	/** 人气指数 **/
	@Column(length = 20, nullable = true)
	private Long clickcount;

	/** 销售量 **/
	@Column(length = 20, nullable = true)
	private Long sellcount;

	/** 是否推荐 **/
	@Column(nullable = true)
	private Boolean commend;

	/** 品牌 **/
	@ManyToOne(cascade=CascadeType.REFRESH, optional=false)
	@JoinColumn(name="brand_id")
	private Brand brand;

	/** 产品类型 **/
	@ManyToOne(cascade=CascadeType.REFRESH, optional=false)
	@JoinColumn(name="producttype_id")
	private ProductType productType;

	/** 产品样式 **/
	@OneToMany(cascade={CascadeType.REMOVE,CascadeType.PERSIST}, mappedBy="productinfo")
	@OrderBy("visible desc, id asc")
	private List<ProductStyle> productStyles = new ArrayList<ProductStyle>();
	
	/** 临时产品扩展信息 **/
	@OneToMany(cascade={CascadeType.REMOVE,CascadeType.PERSIST}, mappedBy="productinfo")
	private List<ProductExtend> productExtends = new ArrayList<ProductExtend>(); 
	
	/** 图片 **/
	@ElementCollection()
	@CollectionTable(name="tb_productinfo_image", joinColumns=@JoinColumn(name="productinfo_id"))
	private List<ImageFile> imageFiles = new ArrayList<ImageFile>();
	
	public void addImageFile(ImageFile image){
		imageFiles.add(image);
	}
	
	public Long getId() {
    	return id;
    }

	public void setId(Long id) {
    	this.id = id;
    }

	public String getCode() {
    	return code;
    }

	public void setCode(String code) {
    	this.code = code;
    }

	public String getName() {
    	return name;
    }

	public void setName(String name) {
    	this.name = name;
    }

	public String getModel() {
    	return model;
    }

	public void setModel(String model) {
    	this.model = model;
    }

	public Float getBaseprice() {
    	return baseprice;
    }

	public void setBaseprice(Float baseprice) {
    	this.baseprice = baseprice;
    }

	public Float getMarketprice() {
    	return marketprice;
    }

	public void setMarketprice(Float marketprice) {
    	this.marketprice = marketprice;
    }

	public Float getSellprice() {
    	return sellprice;
    }

	public void setSellprice(Float sellprice) {
    	this.sellprice = sellprice;
    }

	public Long getWeight() {
    	return weight;
    }

	public void setWeight(Long weight) {
    	this.weight = weight;
    }

	public String getDescription() {
    	return description;
    }

	public void setDescription(String description) {
    	this.description = description;
    }

	public String getBuyexplain() {
    	return buyexplain;
    }

	public void setBuyexplain(String buyexplain) {
    	this.buyexplain = buyexplain;
    }

	public Boolean getVisible() {
    	return visible;
    }

	public void setVisible(Boolean visible) {
    	this.visible = visible;
    }

	public Date getCreatedate() {
    	return createdate;
    }

	public void setCreatedate(Date createdate) {
    	this.createdate = createdate;
    }

	public Long getClickcount() {
    	return clickcount;
    }

	public void setClickcount(Long clickcount) {
    	this.clickcount = clickcount;
    }

	public Long getSellcount() {
    	return sellcount;
    }

	public void setSellcount(Long sellcount) {
    	this.sellcount = sellcount;
    }

	public Boolean getCommend() {
    	return commend;
    }

	public void setCommend(Boolean commend) {
    	this.commend = commend;
    }

	public Brand getBrand() {
    	return brand;
    }

	public void setBrand(Brand brand) {
    	this.brand = brand;
    }

	public List<ProductStyle> getProductStyles() {
    	return productStyles;
    }

	public void setProductStyles(List<ProductStyle> productStyles) {
    	this.productStyles = productStyles;
    }

	public List<ProductExtend> getProductExtends() {
		return productExtends;
	}

	public void setProductExtends(List<ProductExtend> productExtends) {
		this.productExtends = productExtends;
	}

	
	public ProductType getProductType() {
    	return productType;
    }

	public void setProductType(ProductType productType) {
    	this.productType = productType;
    }

	public ProductInfo() {
		super();
	}

	public List<ImageFile> getImageFiles() {
		return imageFiles;
	}

	public void setImageFiles(List<ImageFile> imageFiles) {
		this.imageFiles = imageFiles;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ProductInfo other = (ProductInfo) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
}
